﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.IO;

namespace uacars
{
    
    public partial class frmFactura : Form
    {
        private DataTable dtFactura;
        public frmFactura()
        {
            InitializeComponent();
        }

        public frmFactura(DataTable tabla)
        {
            InitializeComponent();
            dtFactura = tabla;
        }

        private void frmFactura_Load(object sender, EventArgs e)
        {
            tbIdFac.Text = dtFactura.Rows[0]["id"].ToString();
            tbIdPed.Text = dtFactura.Rows[0]["pedido"].ToString();
            tbIdVehiculo.Text = dtFactura.Rows[0]["vehiculo"].ToString();
            tbIdEmpleado.Text = dtFactura.Rows[0]["empleado"].ToString();
            tbIdCliente.Text = dtFactura.Rows[0]["cliente"].ToString();
            tbIdChofer.Text = dtFactura.Rows[0]["chofer"].ToString();
            tbTipoDeTransaccion.Text = dtFactura.Rows[0]["tipotransaccion"].ToString();
            tbFormaDePago.Text = dtFactura.Rows[0]["formadepago"].ToString();
            tbPrecioBruto.Text = dtFactura.Rows[0]["precio"].ToString();
            tbPrecio.Text = dtFactura.Rows[0]["preciofin"].ToString();
            dtpFecha.Value = DateTime.Parse(dtFactura.Rows[0]["fecha"].ToString());
            tbOpcion1.Text = dtFactura.Rows[0]["opcion1"].ToString();
            tbOpcion2.Text = dtFactura.Rows[0]["opcion2"].ToString();
            tbOpcion3.Text = dtFactura.Rows[0]["opcion3"].ToString();
            tbOpcion4.Text = dtFactura.Rows[0]["opcion4"].ToString();
            tbOpcion5.Text = dtFactura.Rows[0]["opcion5"].ToString();
            tbOpcion6.Text = dtFactura.Rows[0]["opcion6"].ToString();

            



        }

        private void textBox15_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox14_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonPDF_Click(object sender, EventArgs e)
        {

            Document pdfDocument = new Document();
            PdfWriter writer = PdfWriter.GetInstance(pdfDocument, new FileStream("c:\\facturaTMP.pdf", FileMode.Create));
            iTextSharp.text.Rectangle r=new iTextSharp.text.Rectangle(2480,3508);
            pdfDocument.SetPageSize(r);


            pdfDocument.Open();
            
            //Uri u=new Uri("http://img149.imageshack.us/img149/3571/facturafinal.jpg");
            //im.Url = u;
            //iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(u);
            iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance("http://img149.imageshack.us/img149/3571/facturafinal.jpg");
            //iTextSharp.text.Image im = iTextSharp.text.Image.GetInstance(@"C:\Documents and Settings\Miguel\Escritorio\facturaFinal.jpg");
            //iTextSharp.text.Image.GetInstance(
            //im.SetAbsolutePosition(pdfDocument.PageSize.Width - 36f - 72f, pdfDocument.PageSize.Height - 36f - 216.6f);
            //pdfDocument.Add(im);

            float X = 0;
            float Y=0;

            if (im.Width > r.Width)
            {
                im.ScaleToFit(r.Width, r.Height);
                X = (r.Width - im.ScaledWidth) / 2;
                Y = (r.Height - im.ScaledHeight) / 2;
            }
            else
            {
                if(im.Height > r.Height)
                {
                    X = (r.Width - im.Width) / 2;
                    Y = (r.Height - im.Height) / 2;
                }
            }
           
            im.SetAbsolutePosition(X, Y);



            PdfContentByte cb = writer.DirectContent;
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb.SetFontAndSize(bf, 80);
            cb.BeginText();

            cb.SetTextMatrix(1792, 2992);
            cb.ShowText(tbIdFac.Text.ToString());
            cb.SetTextMatrix(216, 2700);
            cb.ShowText("Pedido nº "+tbIdPed.Text.ToString());
            cb.SetTextMatrix(216, 2620);
            cb.ShowText("Vehiculo nº "+tbIdVehiculo.Text.ToString());
            cb.SetTextMatrix(216, 2540);
            cb.ShowText("Empleado nº "+tbIdEmpleado.Text.ToString());
            cb.SetTextMatrix(216, 2460);
            cb.ShowText("Cliente nº "+tbIdCliente.Text.ToString());
            cb.SetTextMatrix(216, 2380);
            cb.ShowText("Chofer nº " + tbIdChofer.Text.ToString());
            


            cb.EndText();



            pdfDocument.Close();


            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader("c:\\facturaTMP.pdf");
            iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, new System.IO.FileStream("c:\\factura-UACARS.pdf", FileMode.Create));
            iTextSharp.text.pdf.PdfContentByte underContent = new iTextSharp.text.pdf.PdfContentByte(null);

            underContent = stamper.GetUnderContent(1);
            underContent.AddImage(im);
            stamper.Close();
            reader.Close();
        }


        
    }
}
